<div class="container-fluid" style="margin-top: 10px;">

    <div class="row">
        <div class="col-sm-12">
            {:form('text',['title'=>'用户','name'=>'USER_NAME'])}
            <input hidden name="USER_ID">
            {:form('select',['title'=>'角色','name'=>'ROLE_ID','list'=>$roleList,'key'=>'ROLE_ID','field'=>'NAME'])}
            {:line()}
            <div class="col-sm-6">
                <label>隶属单位数据范围</label>
                <table id="belong_table" class="table" ></table>
            </div>
            <div class="col-sm-6">
                <label>学院数据范围</label>
                <table id="college_table" class="table" ></table>
            </div>
            <div class="col-sm-12" style="text-align: center;margin-top: 10px;">
                <button id="addButton" class="btn btn-info">新增</button>
            </div>
        </div>

    </div>

</div>

<script>

    $(function () {

        $("#belong_table").bootstrapTable({
            url:"{:url('dict/getBelongList')}",
            method:'post',
            columns:[{
                checkbox:true
            },{
                title:'单位',
                field:'BELONG'
            },{
                title:'BELONG_ID',
                field:'BELONG_ID',
                visible: false,
            }],
            queryParams:function (params) {
                return params;
            },
            responseHandler:function (res) {
                // res = JSON.parse(res);

                return res;
            },
        });

        $("#college_table").bootstrapTable({
            url:"{:url('dict/getCollege')}",
            method:'post',
            height:400,
            columns:[{
                checkbox:true,
                clickToSelect:true
            },{
                title:'学院',
                field:'SHORTNAME'
            },{
                title:'COLLEGE_ID',
                field:'COLLEGE_ID',
                visible: false,
            }],
            queryParams:function (params) {
                return params;
            },
            responseHandler:function (res) {
                // res = JSON.parse(res);

                return res;
            }
        });

        $("#addButton").on('click',function () {
            var userId = $("[name='USER_ID']").val();
            var roleId = $("[name='ROLE_ID']").val();
            if (userId.length==0) {
                layer.alert('请选择用户',{icon:2});
                return;
            }
            if (roleId.length==0) {
                layer.alert('请选择角色',{icon:2});
                return;
            }

            var belongList = $("#belong_table").bootstrapTable("getSelections");
            if(belongList.length < 1){
                layer.alert('请勾选隶属单位数据范围',{icon:2});
                return;
            }
            var belongIds = [];
            for(var i = 0 ; i < belongList.length ; i++){
                belongIds.push(belongList[i]['BELONG_ID']);
            }
            console.log(belongIds);

            var collegeList = $("#college_table").bootstrapTable("getSelections");
            if(collegeList.length < 1){
                layer.alert('请勾选学院数据范围',{icon:2});
                return;
            }
            var collegeIds = [];
            for(var i = 0 ; i < collegeList.length ; i++){
                collegeIds.push(collegeList[i]['COLLEGE_ID']);
            }
            console.log(collegeIds);

            layer.confirm('确认新增管理员吗？',{icon:1},function (index) {
                layer.close(index);
                ajax("{:url('addUserRole')}",{userId:userId,roleId:roleId,belongIds:belongIds,collegeIds:collegeIds},function (data) {
                    // console.log(data);
                    parent.location.reload();
                })
            })
        });

        $("input[name='USER_NAME']").click(function () {
            var w = $(window).width()*0.8;
            var h = $(window).height()*0.8;

            layer.open({
                title:'选择用户',
                type:2,
                area:[w+'px',h+'px'],
                content:"{:url('chooseUserPage')}"
            })

        })
    });

    function selectUser(staffId,userName) {
        $("input[name='USER_ID']").val(staffId);
        $("input[name='USER_NAME']").val(userName);
    }


</script>